package com.bsoft.template.service.process;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bsoft.template.common.Result;
import com.bsoft.template.common.auth.UserInfo;
import com.bsoft.template.entity.assets.AssetsZj;
import com.bsoft.template.entity.process.Approvalinfo;
import com.bsoft.template.entity.process.Process;
import com.bsoft.template.mapper.process.ApprovalinfoMapper;
import com.bsoft.template.service.assets.AssetsZjService;
import com.bsoft.template.service.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApprovalinfoService extends BaseServiceImpl<ApprovalinfoMapper, Approvalinfo> {

    @Autowired
    private ProcessService processService;

    @Autowired
    private AssetsZjService assetsZjService;

    @Autowired
    private UserInfo userInfo;

    public Approvalinfo getApprovalinfo(String pzh) {
        QueryWrapper<Approvalinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("pzh", pzh).orderByDesc("spid");
        List<Approvalinfo> list = baseMapper.selectList(wrapper);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    public Result updateAudit(Approvalinfo approvalinfo) {
        String roleCode = userInfo.getRoleCode();
        Process process = processService.getProcessByRole(roleCode);
        approvalinfo.setLcbh(process.getLcbh());
        approvalinfo.setJdid(process.getJdid());
        approvalinfo.setSpjs(roleCode);
        approvalinfo.setSpr(userInfo.getUsername());
        approvalinfo.setSpsj(new Date());
        if ("buzhang".equals(roleCode) && "1".equals(approvalinfo.getSpzt())) {
            AssetsZj assetsZj = new AssetsZj();
            assetsZj.setPzh(approvalinfo.getPzh());
            assetsZj.setYybfzr(userInfo.getUsername());
            assetsZjService.updateById(assetsZj);
        }
        if ("fuzong".equals(roleCode) && "1".equals(approvalinfo.getSpzt())) {
            AssetsZj assetsZj = new AssetsZj();
            assetsZj.setPzh(approvalinfo.getPzh());
            assetsZj.setDwfzr(userInfo.getUsername());
            assetsZjService.updateById(assetsZj);
        }
        return save(approvalinfo);
    }

}
